package com.haier.waterdevice;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * activity 基类 Created by ZhengChengwei on 2016/7/21.
 */
public class BaseActivity extends AppCompatActivity {

    // protected ActionBar actionBar;
    protected Toolbar toolbar;
    private View contentView;
    private LinearLayout rootView;
    private FrameLayout barLayout;
    private TextView titleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        /*
         * actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setElevation(0);
         * actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowHomeEnabled(false);
         * //隐藏图标 actionBar.setHomeAsUpIndicator(R.drawable.icon_back); }
         */
        rootView = (LinearLayout) getLayoutInflater().inflate(R.layout.toolbar,
                null);
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        barLayout = (FrameLayout) rootView.findViewById(R.id.bar_layout);
        titleTv = (TextView) rootView.findViewById(R.id.title_tv);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.icon_back);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        contentView = LayoutInflater.from(this).inflate(layoutResID, null);
        rootView.addView(contentView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        super.setContentView(rootView);
    }

    @Override
    public void setContentView(View contentView) {
        this.contentView = contentView;
        rootView.addView(contentView);
        super.setContentView(rootView);
    }

    /**
     * 隐藏toolbar
     */
    public void hideBar() {
        barLayout.setVisibility(View.GONE);
    }

    /**
     * 显示toolbar
     */
    public void showBar() {
        barLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 设置返回按钮是否可用
     *
     * @param backEnable
     */
    public void setBackEnable(boolean backEnable) {
        if (backEnable) {
            toolbar.setNavigationIcon(R.drawable.icon_back);
        } else {
            toolbar.setNavigationIcon(null);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        titleTv.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        String titleStr = getResources().getString(titleId);
        titleTv.setText(titleStr);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
